/**
 * Role management 
 */
var _roleIdU;
var _roleIdR;
var _roleId;
var role = {

	/**
	 * Show role list
	 */
	list : function() {
		// Request role list url from RoleController.
		var roleListUrl = baseURL + "/system/role/list"
		$.ajax({
			type : "get",
			url : roleListUrl,
			dataType : "html",
			success : function(html) {
				// Request successfully and fill the data.
				$("#list").empty().html(html);
			}
		});
	},
		
	/**
	 * Edit role
	 */
	edit : function() {
		// Get the first userId from checked buttons.
		var select = new Array();;
		$("input[name='id']:checked").each(function(){
			select.push($(this).val()); 
            }); 

		if (select.length == 0) {
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}
		else if(select.length>1){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}
		else{
			// Get the first roleId from checked buttons.
			var roleId = $("input[name='id']:checked").eq(0).val();
			_roleId = roleId;
			var param = {};
			// If userId is not empty, edit the user info; or new it.
			var title = localeMsg.role.edit;
			param.roleId = roleId;
			common.dialog({
				obj : "#editRoleDialog",
				url : baseURL + "/system/role/edit",
				param : param,
				title : title,
				width : 400,
				confirm : function() {
					if($("#editRoleForm").form('validate')==true){
						// Click the "Confirm" button to save user info.
						role.save();
					}
					else{
						common.showTip({
							title : localeMsg.common.warn,
							text : localeMsg.common.invalidData
						});
					}
						

				}
			});
		}

	},
	
	/**
	 * New role
	 */
	create : function() {
		// Get the first roleId from checked buttons.
		var roleId = {};
		_roleId = "";
		// The title of edit dialog
		var title = localeMsg.role.create;
		var param = {};
		// If userId is not empty, edit the user info; or new it.

		common.dialog({
			obj : "#editRoleDialog",
			url : baseURL + "/system/role/edit",
			param : param,
			title : title,
			width : 400,
			confirm : function() {
				if($("#editRoleForm").form('validate')==true){
					// Click the "Confirm" button to save user info.
					role.save();
				}
				else{
					common.showTip({
						title : localeMsg.common.warn,
						text : localeMsg.common.invalidData
					});
				}
					

			}
		});
	},
	
	/**
	 * Save roles
	 */
	save : function() {
		$("#editRoleForm").ajaxSubmit({
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						success : true,
						title : localeMsg.common.info,
						text : localeMsg.common.saveSuccess,
						callback : function() {
							role.list();
						}
					});
				}
				else {
					if (json.result && json.result == "failure"){
						common.showTip({
							success : false,
							title : localeMsg.common.warn,
							text : localeMsg.common.saveFailed,
						});
					}
					else{
						common.showTip({
							success : false,
							title : localeMsg.common.warn,
							text : localeMsg.role.neverdelete,
						});
					}
				}
			}
		});
	},
	
	
	/**
	 * Remove roles confirm
	 */
	remove : function(){
		var roleId=[];
		$('input[name="id"]:checked').each(function(){
			roleId.push($(this).val()); 
            });  
		if (Object.keys(roleId).length === 0){
			common.showTip({
				title : localeMsg.common.warn,
				text : localeMsg.common.selectMsg
			});
		}else{
			var confirmMessage = localeMsg.common.relateddeleteconfirm;
			common.confirmDialog(confirmMessage,role.removeDel);
		}

	},
	
	
	/**
	 * Remove roles
	 */
	removeDel : function() {
		//  Remove  records
		var roleId=[];
		$('input[name="id"]:checked').each(function(){
			roleId.push($(this).val()); 
            });  
		
		$.ajax({
			type : "post",
			url : baseURL + "/system/role/remove",
			data : {roleId : roleId},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						success : true,
						title : localeMsg.common.info,
						text : localeMsg.common.deleteSuccess,
						callback : function() {
							role.list();
						}
					});
				}
				else {
					if (json.result && json.result == "failure"){
						common.showTip({
							success : false,
							title : localeMsg.common.warn,
							text : localeMsg.common.deleteFailed,
						});
					}
					else{
						common.showTip({
							success : false,
							title : localeMsg.common.warn,
							text : localeMsg.role.neverdelete,
						});
					}
				}
			}
		});
	},
	

	
	/**
	 * Edit roleUsers
	 */
	edituser : function(value) {

		var roleId =value;
		_roleIdU=value;
		var param = {};
		var title = localeMsg.role.edit_users;
		param.roleId = roleId;
		common.dialog({
			obj : "#editRoleUserForm",
			url : baseURL + "/system/role/edituser",
			param : param,
			title : title,
			width : 400,
			height: 400,
			confirm : function(){
				// Click the "Confirm" button to save user info.
				role.saveuser();
			}
		});
	},
	

	/**
	 * Save Role Users
	 */
	saveuser : function() {
		var roleId;
		var oldUserId=[];
		var newUserID=[];
		roleId = _roleIdU;
		_roleIdU = "";
			
		$('#edituser input[name="id"]:checked').each(function(){
			newUserID.push($(this).val()); 
	           });  
		
		$('#edituser input[id="SELECT"]').each(function(){
			oldUserId.push($(this).val()); 
	           });  
		
		$.ajax({
			type : "post",
			url : baseURL + "/system/role/saveuser",
			data : {newUserId :newUserID , oldUserId : oldUserId , roleId : roleId},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						success : true,
						title : localeMsg.common.info,
						text : localeMsg.common.saveSuccess,
					});
				} else {
					common.showTip({
						success : false,
						title : localeMsg.common.warn,
						text : localeMsg.common.saveFailed,
					});
				}
			}
			
		});
	},

	/**
	 * Edit roleResources
	 */
	editresource : function(value){
		var roleId =value;
		_roleIdR=value;
		var param = {};
		var title = localeMsg.role.edit_resource;
		param.roleId = roleId;
		common.dialog({
			obj : "#editRoleResourceForm",
			url : baseURL + "/system/role/editresource",
			param : param,
			title : title,
			width : 400,
			height: 400,
			confirm : function(){
				// Click the "Confirm" button to save user info.
				role.saveresource();
			}
		});
	},
	
	
	ResourceTreeInit :function (){
		var resourceTree = baseURL + "/system/role/resourceTree"
		$.ajax({
			type : "get",
			url : resourceTree,
			data : {roleId : _roleIdR},
			dataType : "json",
			success : function(json){
				$('#resourceTree').tree({
					data : [json],
					animate : true,
					checkbox : true,
					cascadeCheck : false
				})
			}
		});
	},
	
	saveresource : function(){
		var roleId;
		var newResourceID=[];
		roleId =_roleIdR;
		_roleIdR = "";
			
		var nodes=$('#resourceTree').tree('getChecked');
		for(var x in nodes){
			newResourceID.push(nodes[x].id);
		}
		
		$.ajax({
			type : "post",
			url : baseURL + "/system/role/saveresource",
			data : {newResourceId :newResourceID , roleId : roleId},
			traditional: true,
			dataType : "json",
			success : function(json) {
				if (json.result && json.result == "success") {
					common.showTip({
						success : true,
						title : localeMsg.common.info,
						text : localeMsg.common.saveSuccess,
					});
				} else {
					common.showTip({
						success : false,
						title : localeMsg.common.warn,
						text : localeMsg.common.saveFailed,
					});
				}
			}
			
		});
	},
	/**
	 *表单数据验证
	*/
	
	validate : function(){
		
	    $('#input_roleValue').validatebox({
	        required: true,
	        validType: ['maxLength[50]']
	    });
	    
	    $('#input_roleName').validatebox({
	        required: true,
	        validType: ['maxLength[50]','vername','uniquename["/system/role/validateName","'+_roleId+'"]']
	    });
	    

	},
};
